# Maintainer: umarcor <unai.martinezcorral@ehu.eus>

_realname=verilator
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.034
pkgrel=1
pkgdesc="The fastest free Verilog HDL simulator (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.veripool.org/verilator/"
msys2_repository_url="https://github.com/verilator/verilator"
license=("spdx:LGPL-3.0-only")
groups=("${MINGW_PACKAGE_PREFIX}-eda")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-python"
             "help2man")
# Will not build correctly if perl-w64 is installed on the machine!
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs" "perl")

source=("https://github.com/${_realname}/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
"0001-Use-msys2-perl-since-it-doesn-t-work-with-windows-pe.patch"
)
sha256sums=('002da98e316ca6eee40407f5deb7d7c43a0788847d39c90d4d31ddbbc03020e8'
            '36e4268d4af11dcf15277a212f6dd0e5b05d4f98eafe381d46768c67aa90926c')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -Np1 -i "${srcdir}/0001-Use-msys2-perl-since-it-doesn-t-work-with-windows-pe.patch"
  cp /usr/include/FlexLexer.h src/
  autoconf
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  cp -rf "${srcdir}"/${_realname}-${pkgver} "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  export MSYS2_ARG_CONV_EXCL="-DDEFENV"

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST}

  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
}
